package com.anywhere.freightman.config;

import com.anywhere.freightman.common.ContextKeys;
import io.swagger.annotations.Api;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.ArrayList;
import java.util.List;

@Configuration
public class Swagger2Config {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                .paths(PathSelectors.any())
                .build()
                .securitySchemes(securitySchemes());
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                //页面标题
                .title("货运系统")
                //创建人
                .contact(new Contact("Michael", "http://www.anywhere.com", "jia_michael@163.com"))
                //版本号
                .version("1.0")
                //描述
                .description("货运系统所需接口资源")
                .build();
    }

    @Bean
    public ApiKey buildApiKey() {
        return new ApiKey(ContextKeys.TOKEN_KEY, ContextKeys.TOKEN_KEY, "header");
    }

    private List<ApiKey> securitySchemes() {
        ApiKey apiKey = new ApiKey(ContextKeys.TOKEN_KEY, ContextKeys.TOKEN_KEY, "header");
        List<ApiKey> list = new ArrayList<>();
        list.add(apiKey);
        return list;
    }
}
